﻿define(['admin/app', "service/admin/settingService"], function (app) {
    "use strict";

    app.controller('settingController', function ($scope, $stateParams, $location, $state, utils, settingService) {
        $scope.psdisabled = false;
        var methods = {
            search: function () {
                settingService.get({ _code: $stateParams.action }).success(function (response) {
                    $scope.model = response.data;
                });
            },
            tokenSetting: function () {
                settingService.getTokenSetting({ _code: $stateParams.action }).success(function (response) {
                    $scope.model = response.data;
                });
            },
            saveTokenSetting: function () {
                var model = $scope.model;
                $scope.psdisabled = true;
                settingService.saveTokenSetting(model).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            save: function () {
                settingService.put($scope.model, { _code: $stateParams.action }).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000)
                        methods.search();
                    else
                        $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            post: function () {
                $scope.model._code = $stateParams.action;
                settingService.post($scope.model).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000)
                        methods.search();
                    else
                        $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            add_payment: function () {
                $scope.detail_panel = true;
                $scope.item = { code: '', name: '', settles: [] };
            },
            edit_payment: function (item) {
                $scope.detail_panel = true;
                $scope.item = {};
                $.extend($scope.item, item);
            },
            remove_payment: function (item) {
                $scope.detail_panel = false;
                var modal = utils.confirm({ msg: "是否需要删除当前项目", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    utils.remove($scope.model, item);
                    methods.save();
                });
            },
            add_settle: function () {
                $scope.item.settles.push({ settle_type: '' });
            },
            remove_settle: function (settle) {
                utils.remove($scope.item.settles, settle);
            },
            save_payment: function () {
                var selected = $.grep($scope.model, function (n, i) {
                    return n.code == $scope.item.code;
                });
                if (selected.length > 0) {
                    selected[0].name = $scope.item.name;
                    selected[0].logo = $scope.item.logo;
                    selected[0].is_open = $scope.item.is_open;
                    selected[0].pay_rate = $scope.item.pay_rate;
                    selected[0].payment_config = $scope.item.payment_config;
                }
                else $scope.model.push($scope.item);
                $scope.detail_panel = false;
                methods.save();
            },
            getTokenAppSetting: function () {
                settingService.getTokenAppSetting().success(function (response) {
                    $scope.model = response.data;
                });
            },
            saveTokenAppSetting: function () {
                var model = $scope.model;
                $scope.psdisabled = true;
                settingService.saveTokenAppSetting(model).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            getTokenSMSSetting: function () {
                settingService.getTokenSMSSetting().success(function (response) {
                    $scope.model = response.data;
                });
            },
            saveTokenSMSSetting: function () {
                var model = $scope.model;
                $scope.psdisabled = true;
                settingService.saveTokenSMSSetting(model).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            getTokenWithdrawSetting: function () {
                settingService.getTokenWithdrawSetting().success(function (response) {
                    $scope.model = response.data;
                });
            },
            saveTokenWithdrawSetting: function () {
                var model = $scope.model;
                $scope.psdisabled = true;
                settingService.saveTokenWithdrawSetting(model).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            createTokenTestUserView: function () {
                settingService.createTokenTestUserView().success(function (response) {
                    $scope.model = response.data;
                });
            },
            createTokenTestUser: function () {
                var model = $scope.model;
                $scope.psdisabled = true;
                settingService.createTokenTestUser(model).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    $scope.psdisabled = false;
                    $scope.model.mobileSuffix = "";
                }).error(function () { $scope.psdisabled = false; });
            },
            getTokenTransferSetting: function () {
                settingService.getTokenTransferSetting().success(function (response) {
                    $scope.model = response.data;
                });
            },
            saveTokenTransferSetting: function () {
                var model = $scope.model;
                $scope.psdisabled = true;
                settingService.saveTokenTransferSetting(model).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            getTokenMallShoppingSetting: function () {
                settingService.getTokenMallShoppingSetting().success(function (response) {
                    $scope.model = response.data;
                });
            },
            saveTokenMallShoppingSetting: function () {
                var modal = utils.confirm({ msg: "您确定要保存么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    var model = $scope.model;
                    $scope.psdisabled = true;
                    settingService.saveTokenMallShoppingSetting(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                });
            },
            getTokenOpenSupplierSetting: function () {
                settingService.getTokenOpenSupplierSetting().success(function (response) {
                    $scope.model = response.data;
                });
            },
            saveTokenOpenSupplierSetting: function () {
                var modal = utils.confirm({ msg: "您确定要保存么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    var model = $scope.model;
                    $scope.psdisabled = true;
                    settingService.saveTokenOpenSupplierSetting(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                });
            },
        }
        $scope.action = $stateParams.action;
        switch ($stateParams.action) {
            case "site":
                $scope.breadcrumb = "站点设置";
                break;
            case "payments":
                $scope.breadcrumb = "支付方式";
                break;
        }

        if ($state.includes('app.token.token_setting')) {
            methods.tokenSetting();
        } else if ($state.includes('app.token.token_appsetting')) {
            methods.getTokenAppSetting();
        } else if ($state.includes('app.token.token_smssetting')) {
            methods.getTokenSMSSetting();
        }
        else if ($state.includes('app.token.token_withdrawsetting')) {
            methods.getTokenWithdrawSetting();
        } else if ($state.includes('app.token.token_transfersetting')) {
            methods.getTokenTransferSetting();
        } else if ($state.includes('app.token.token_createuser')) {
            methods.createTokenTestUserView();
        } else if ($state.includes('app.token.token_mallshoppingsetting')) {
            methods.getTokenMallShoppingSetting();
        } else if ($state.includes('app.token.token_opensuppliersetting')) {
            methods.getTokenOpenSupplierSetting();
        }else {
            methods.search();
        }

        angular.extend($scope, methods);
    });
});
